package com.lzw.bluetoothpractice.Activitys;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.lzw.bluetoothpractice.Adapters.HealthyMessagesAdapter;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.Models.Article;
import com.lzw.bluetoothpractice.Models.Collect;
import com.lzw.bluetoothpractice.Models._User;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.bean.HealthyMessageBean;
import com.lzw.bluetoothpractice.bean.HealthyMessageListBean;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.util.List;
import java.util.Map;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import io.reactivex.disposables.Disposable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.lzw.bluetoothpractice.MyApplication.getContext;

public class HealthyDetailActivity extends BaseActivity {
    private Map<String, String> keyMap;
    private  Intent intent;
    TextView healthy_title;
    TextView tv_userName;
    TextView create_time;
    TextView messages_count;
    TextView healthy_mesage;

    ImageView iv_head;
    ImageView guanfang;
    ImageView msg_pic;
    ImageButton colect;
    Long articleId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_healthy_detail);
        intent=getIntent();
        articleId= (Long) intent.getSerializableExtra("articleID");

       // Log.e("article",article.toString());
        InitNavBar(true,"健康资讯",false);
        InitView();
        InitData();


    }
    public void InitView(){
        healthy_title=fd(R.id.healthy_title);
        tv_userName=fd(R.id.tv_userName);
        create_time=fd(R.id.create_time);
        messages_count=fd(R.id.messages_count);
        healthy_mesage=fd(R.id.healthy_mesage);
        iv_head=fd(R.id.iv_head);
        guanfang=fd(R.id.guanfang);
        colect=fd(R.id.colect);
        msg_pic=fd(R.id.msg_pic);


    }
    public  void InitData(){
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/message/messageId");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String accessToken = SharePreferenceUtils.getString(getContext(),"access_token",null);;
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).getMessageDetail(articleId,"1", AppUtil.getVersionCode() + "", accessToken, keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("资讯详细查询",e.getMessage()+"");

            }

            @Override
            public void onNext(JSONObject json) {


                HealthyMessageBean homeGridBean = JSON.parseObject(json.toString(), HealthyMessageBean.class);
                if(homeGridBean.getCode()==200){

                    if(!homeGridBean.getData().getMessagePic().isEmpty()){
                        msg_pic.setVisibility(View.VISIBLE);
                        Glide.with(HealthyDetailActivity.this).load(AppConfig.BASE_URL+homeGridBean.getData().getMessagePic()).into(msg_pic);

                    }
                    messages_count.setText(homeGridBean.getData().getCollectCnt()+"");
                    healthy_title.setText(homeGridBean.getData().getMessageTitle());
                    create_time.setText(homeGridBean.getData().getCreateTime());
                    healthy_mesage.setText(Html.fromHtml(homeGridBean.getData().getMessageContent()));
                    healthy_mesage.setMovementMethod(ScrollingMovementMethod.getInstance());


                }else {
                    Toast toast=Toast.makeText(HealthyDetailActivity.this,"查询健康资讯失败",Toast.LENGTH_LONG);
                    toast.show();
                }


            }
        });



    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
       // isCltSatus=false;
      //  clt=false;
    }


}
